package ModelLayer;

import java.util.ArrayList;

/**
 * Thie class represents a collection of DVD's
 * 
 * @author Sebastian K Munch    
 * @version 6/11 -12
 */
public class DvdCollection
{
    private ArrayList<DVD>  dvdList;
    private static DvdCollection instance;

    /**
     * Constructor for objects of class DvdCollection.
     */
    private DvdCollection()
    {
        dvdList = new ArrayList<DVD>();
    }
    
    /**
     * This method the instance of this class, in acordance with the singleton implementation.
     */
    public static DvdCollection getInstance()
    {
        if(instance == null)
        {
            instance = new DvdCollection();
        }
        return instance;
    }
    
    /**
     * This method searches the list of DVDs associated with this class, and returns a DVD with the given id. If none are found, null is returned.
     * 
     * @param id    The id of the DVD to be found. A long.
     * 
     * @return  A DVD with the given id, as a DVD.
     */
    private DVD findDvd(long id)
    {
        int i = 0;
        while(i<dvdList.size())
        {
            if(dvdList.get(i).getID()==id)
            {
                return dvdList.get(i);
            }
            i++;
        }
        return null;
    }
    
    /**
     * This nethod creates a new object of the class DVD, and associates it with this class.
     * 
     * @param   id          The id of the dvd to be creater. A long.
     * @param   title       The title of the dvd to be created. A String.
     * @param   director    The director of the dvd to be created. A String.
     * @param   releasDate  The release date of the dvd to be created. A String.
     */
    public void createDvd(long id, String title, String director, String releaseDate)
    {
        dvdList.add(new DVD(id,title,director,releaseDate));
    }
    
    /**
     * This method creates a new copy of a DVD.
     * 
     * @param   id                  The id of the DVD to witch the Copy is to be added. A long.
     * @param   serialNumber        The serial number of the Copy to be created. A long.
     * @param   acquisitionDate     The date on witch the copy was acquired. A String.
     * @param   acquisitionPrice    The price for witch the copy was aquired. A String.
     */
    public void createCopy(long id, String serialNumber, String acquisitionDate, double acquisitionPrice)
    {
        findDvd(id).addCopy(serialNumber, acquisitionDate, acquisitionPrice);
    }
    
    /**
     * This method returns a DVD with the specified id.
     * 
     * @param   id  The id of the DVD. A long.
     */
    public DVD getDVD(long id)
    {
        return findDvd(id);
    }
    
    /**
     * This method returns a Copy of a DVD.
     * 
     * @param   id          The id of the DVD. A long.
     * @param   serialNumber    The serial number of the Copy. A long.
     */
    public Copy getCopy(long id, String serialNumber)
    {
        return findDvd(id).findCopy(serialNumber);
    }
    
    /**
     * This method updates all the atributes of a DVD.
     * 
     * @param   id          The id of the dvd. A long.
     * @param   title       The title of the dvd. A String.
     * @param   director    The director of the dvd. A String.
     * @param   releasDate  The release date of the dvd. A String.
     */
    public void updateDVD(long id, String title, String director, String releaseDate)
    {
        DVD d = findDvd(id);
        d.setTitle(title);
        d.setDirector(director);
        d.setReleaseDate(releaseDate);
    }
    
    /**
     * This method deletes a DVD
     * 
     * @param   id  The id of the dvd. A long.
     */
    public boolean deleteDVD(long id)
    {
        boolean found = false;
        int i = 0;
        while(i<dvdList.size() && !found)
        {
            if(dvdList.get(i).getID()==id)
            {
                dvdList.remove(i);
                found=true;
            }
            i++;
        }
        return found;
    }
    
    /**
     * This methode deletes a Copy of a DVD.
     * 
     * @param   id              The id of the DVD. A long.
     * @param   serialNumber    The serial number of the copy. A long.
     */
    public boolean deleteCopy(long id, String serialNumber)
    {
        boolean found = false;
        int i = 0;
        while(i<dvdList.size() && !found)
        {
            if(dvdList.get(i).getID()==id)
            {
                dvdList.get(i).deleteCopy(serialNumber);
                found=true;
            }
        }
        return found;
    }
    
    /**
     * This method returns a list of DVDs.
     * 
     * @return  A list of DVD, as ArrayList<DVD>.
     */
    public ArrayList<DVD> listAllDvds()
    {
        return dvdList;
    }
    
    /**
     * This method determines wether a dvd obejct with id exists.
     * 
     * @param   id  The id of the dvd
     * @return  Weather the dvd exist, as a boolean.
     */
    public boolean hasDvd(long id)
    {
        boolean found = false;
        int i = 0;
        while(i<dvdList.size() && !found)
        {
            if(dvdList.get(i).getID()==id)
            {
                found=true;
            }
            i++;
        }
        return found;
    }
}


